package com.lyw.GeoLocationClient.service;

import android.content.Context;
import com.lyw.GeoLocationClient.App;
import com.lyw.GeoLocationClient.utils.LBSUrl;
import com.lyw.GeoLocationClient.json.res.GeotableList;
import com.lyw.GeoLocationClient.json.res.PoiList;
import com.lyw.GeoLocationClient.json.res.Respon;
import retrofit.Callback;
import retrofit.RestAdapter;

import static com.lyw.GeoLocationClient.utils.LogUtils.LOGD;

public class LBSManager {
    public  static final int PROP_TYPE = 3;
    private static final int MAX_POI_SIZE = 200;

//    private HttpAsyncExecutor asyncExecutor;
    private LBSService lbsService;

    public LBSManager(Context context) {
        setupApi(context);
    }

    private void setupApi(Context context) {
        LOG(" setup lbs api service !");
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(LBSUrl.BD_LBS_API)
                .setErrorHandler(new RetrofitErrorHandler(context))
                .build();
        if (App.DEBUG) {
            restAdapter.setLogLevel(RestAdapter.LogLevel.BASIC);
        }
        lbsService = restAdapter.create(LBSService.class);
    }

//    public void createGeotable(String name, String passwd, int type, HttpResponseHandler handler) {
//        CreateGeoTableReq req = new CreateGeoTableReq(name, passwd);
//        req.geotype = type;
//
//        Request request = new Request(LBSUrl.CREATE_GEOTABLE);
//        request.setMethod(HttpMethod.Post);
//        request.setParamModel(req);
//
//        asyncExecutor.execute(request, handler);
//    }

    public void createGeotable(String name, String password, int type, Callback<Respon> handler) {
        lbsService.createTable(name, password, type, 0, App.SRV_AK, handler);
    }

    public void createGeotable(String name, String passwd, Callback<Respon> handler) {
//        CreateGeoTableReq req = new CreateGeoTableReq(name, passwd);
//        Request request = new Request(LBSUrl.CREATE_GEOTABLE);
//        request.setMethod(HttpMethod.Post);
//        request.setParamModel(req);
//
//        asyncExecutor.execute(request, handler);
        createGeotable(name, passwd, 1, handler);
    }

    public void createTableProp(int geotable_id, String title, String value, Callback<Respon> handler) {
        lbsService.createPoi(title, value, null, 0, 0, PROP_TYPE, geotable_id, App.SRV_AK, null, handler);
    }



    public void createPoi(String title,
                          String address,
                          String tags, double lat, double lag, int coord_type, int geotable,
                          String ak, String sn,
                          Callback<Respon> handler) {
        lbsService.createPoi(title, address, tags, lag, lag, coord_type, geotable, ak, sn, handler);
    }

    public void createPoi( String name, double lat, double lon, int coord_type, int geotable,
                          Callback<Respon> handler) {
        lbsService.createPoi(name, null, null, lat, lon, coord_type, geotable, App.SRV_AK, null, handler);
    }

//    public void createTableProp(int geotable_id, String title, String value,
//                                HttpResponseHandler handler) {
//        CreatePoiReq req = new CreatePoiReq();
//        req.latitude = 0;
//        req.longitude = 0;
//        req.coord_type = PROP_TYPE;
//        req.geotable_id = geotable_id;
//        req.title = title;
//        req.tags = value;
//
//        Request request = new Request(LBSUrl.CREATE_POI);
//        request.setMethod(HttpMethod.Post);
//        request.setParamModel(req);
//
//        LOG("createTableProp");
//
//        asyncExecutor.execute(request, handler);
//    }

//    public void getPoiList(String name, HttpResponseHandler httpResponseHandler) {
//        ListPoiReq req = new ListPoiReq();
//        req.geotable_id = LBSClient.GEOTABLE_ID;
//        req.page_size = MAX_POI_SIZE;
//        req.title = name;
//
//        Request request = new Request(LBSUrl.LIST_POI);
//        request.setMethod(HttpMethod.Get);
//        request.setParamModel(req);
//
//        asyncExecutor.execute(request, httpResponseHandler);
//
//    }
//
//    public void getPoiList(final int geotable_id, HttpResponseHandler httpResponseHandler) {
//        ListPoiReq req = new ListPoiReq();
//        req.geotable_id = geotable_id;
//        req.page_size = MAX_POI_SIZE;
//
//        Request request = new Request(LBSUrl.LIST_POI);
//        request.setMethod(HttpMethod.Get);
//        request.setParamModel(req);
//
//        asyncExecutor.execute(request, httpResponseHandler);
//
//    }


//    public void ListGeotable(final String name, final String passwd, HttpResponseHandler handler) {
//        ListGeotableReq req = new ListGeotableReq();
//        req.name = name;
////        req.sn = passwd;
////        req.ak = App.SRV_AK;
//        Request request = new Request(LBSUrl.LIST_GEOTABLE);
//        request.setMethod(HttpMethod.Get);
//        request.setParamModel(req);
//        LOG("ListGeotable " + name);
//        asyncExecutor.execute(request, handler);
//    }
//
//    public void ListGeotable(HttpResponseHandler httpResponseHandler) {
//        ListGeotableReq req = new ListGeotableReq();
//
//        Request request = new Request(LBSUrl.LIST_GEOTABLE);
//        request.setMethod(HttpMethod.Get);
//        request.setParamModel(req);
//
//        asyncExecutor.execute(request, httpResponseHandler);
//    }

    public void listTable(String name, Callback<GeotableList> callback) {
        lbsService.listTable(name, App.SRV_AK, callback);
    }

    public void delPois(String name, Callback<Respon> callback) {
        lbsService.delPois(name, LBSClient.GEOTABLE_ID, App.SRV_AK, 1, callback);
    }
//
//    public void delPoi(int geotable_id, int poi_id, HttpResponseHandler responseHandler) {
//        DelPoiReq req = new DelPoiReq(poi_id, geotable_id);
//        Request request = new Request(LBSUrl.DEL_POIS);
//        request.setMethod(HttpMethod.Post);
//        request.setParamModel(req);
//        asyncExecutor.execute(request, responseHandler);
//    }

    public void listPoi(int geotable, String title, Callback<PoiList> cb) {
        lbsService.listPoi(title, geotable, App.SRV_AK, 200, cb);
    }

    public void listPoiByTime(int geotable, String title, double min, double max, Callback<PoiList> cb) {
        String minS = min == 0 ? "-" : String.valueOf(min);
        String maxS = max == 0 ? "-" : String.valueOf(max);
        String time = minS + "," + maxS;
        lbsService.listPoiByTime(title, time, geotable, App.SRV_AK, 200, cb);
    }

//    public void findPoi(int geotable_id, String title,  HttpResponseHandler responseHandler) {
//        ListPoiReq req = new ListPoiReq();
//        req.geotable_id = geotable_id;
//        req.page_size = MAX_POI_SIZE;
//        req.title = title;
//
//        Request request = new Request(LBSUrl.LIST_POI);
//        request.setMethod(HttpMethod.Get);
//        request.setParamModel(req);
//
//        asyncExecutor.execute(request, responseHandler);
//    }
//
//
//    public void modifyPoi(int geotable_id, int poi_id, String value, HttpResponseHandler responseHandler) {
//        UpdatePoiReq req = new UpdatePoiReq();
//        req.geotable_id = Integer.valueOf(geotable_id);
//        req.id = poi_id;
//        req.tags = value;
//
//        Request request = new Request(LBSUrl.UPDATE_POIS);
//        request.setMethod(HttpMethod.Post);
//        request.setParamModel(req);
//
//        asyncExecutor.execute(request, responseHandler);
//
//    }


    public void LOG(String msg) {
        if (App.DEBUG)
            LOGD(this.getClass(), msg);
    }

}
